<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('work_injury_compensations', function (Blueprint $table) {
            $table->id();
            $table->timestamps();

            // 关联记录
            $table->unsignedBigInteger('work_injury_record_id')->nullable();
            $table->unsignedBigInteger('disability_assessment_id')->nullable();
            $table->string('injury_number', 50)->nullable();

            // 员工信息
            $table->unsignedBigInteger('employee_id');
            $table->string('employee_name', 100)->nullable();
            $table->string('employee_number', 50)->nullable();

            // 补偿基本信息
            $table->string('compensation_number', 50)->unique(); // 补偿编号
            $table->string('compensation_type', 50); // disability, medical, nursing, termination, death, etc.
            $table->string('compensation_category', 50)->nullable(); // monthly, one_time, etc.

            // 补偿金额
            $table->decimal('compensation_amount', 12, 2); // 补偿金额
            $table->string('currency', 10)->default('CNY'); // 货币类型
            $table->decimal('base_salary', 10, 2)->nullable(); // 计算基准工资

            // 补偿标准
            $table->string('compensation_standard', 100)->nullable(); // 补偿标准说明
            $table->decimal('multiplier', 5, 2)->nullable(); // 倍数（如20倍等）
            $table->integer('compensation_months')->nullable(); // 补偿月数

            // 发放信息
            $table->string('payment_status', 20)->default('pending'); // pending, paid, cancelled
            $table->datetime('payment_date')->nullable(); // 发放日期
            $table->string('payment_method', 50)->nullable(); // bank_transfer, cash, etc.
            $table->string('payment_reference', 100)->nullable(); // 支付凭证号

            // 收款人信息
            $table->string('recipient_name', 100); // 收款人姓名
            $table->string('recipient_relationship', 50)->nullable(); // 与员工关系
            $table->string('recipient_id_card', 18)->nullable(); // 收款人身份证
            $table->string('recipient_bank_account', 50)->nullable(); // 银行账号
            $table->string('recipient_contact', 100)->nullable(); // 联系方式

            // 审批信息
            $table->string('approval_status', 20)->default('pending'); // pending, approved, rejected
            $table->unsignedBigInteger('approved_by')->nullable();
            $table->string('approved_by_name', 100)->nullable();
            $table->datetime('approved_at')->nullable();
            $table->text('approval_notes')->nullable();

            // 特殊补偿类型
            $table->boolean('is_termination_compensation')->default(false); // 是否为离职补偿
            $table->boolean('is_disability_compensation')->default(false); // 是否为伤残补偿
            $table->boolean('is_death_compensation')->default(false); // 是否为工亡补偿
            $table->boolean('is_medical_compensation')->default(false); // 是否为医疗补偿

            // 税务信息
            $table->boolean('is_taxable')->default(true); // 是否纳税
            $table->decimal('tax_amount', 10, 2)->default(0); // 税款金额
            $table->decimal('net_amount', 10, 2)->nullable(); // 净金额

            // 财务信息
            $table->string('budget_category', 50)->nullable(); // 预算类别
            $table->string('cost_center', 50)->nullable(); // 成本中心
            $table->string('accounting_period', 20)->nullable(); // 会计期间

            // 备注和附件
            $table->text('calculation_basis')->nullable(); // 计算依据
            $table->text('notes')->nullable();
            $table->json('attachments')->nullable(); // 相关附件

            // 创建者信息
            $table->string('created_by', 100)->nullable();
            $table->string('updated_by', 100)->nullable();

            // 索引
            $table->index(['work_injury_record_id', 'compensation_type'], 'wic_wir_ctype_idx');
            $table->index(['employee_id', 'compensation_type'], 'wic_emp_ctype_idx');
            $table->index(['compensation_type', 'payment_status'], 'wic_ctype_pstatus_idx');
            $table->index(['payment_date'], 'wic_pdate_idx');
            $table->index(['approval_status'], 'wic_astatus_idx');
            $table->index(['is_termination_compensation'], 'wic_term_idx');
            $table->index(['is_disability_compensation'], 'wic_disability_idx');
            $table->index(['is_death_compensation'], 'wic_death_idx');
            $table->index(['compensation_number'], 'wic_cnumber_idx');

            // 外键约束
            $table->foreign('work_injury_record_id')->references('id')->on('work_injury_records')->onDelete('set null');
            $table->foreign('disability_assessment_id')->references('id')->on('work_injury_disability_assessments')->onDelete('set null');
            $table->foreign('employee_id')->references('id')->on('employees')->onDelete('cascade');
            $table->foreign('approved_by')->references('id')->on('users')->onDelete('set null');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('work_injury_compensations');
    }
};
